const puppeteer = require('puppeteer');
const request = require('request');

const post = pic => {
    return new Promise(resolve => request(`https://tusou.vvic.com/upload?searchCity=gz&url=${pic}`,
        (error, response, body) => {
            try {
                if (error) {
                } else {
                    resolve(`https://tusou.vvic.com${JSON.parse(body).data.url}`);
                }
            } catch (e) {
                console.log(e);
            }
        }));
}

const pic = 'https://gd3.alicdn.com/imgextra/i4/2632461002/O1CN011JGxJ9LEtFSv8Pl_!!2632461002.jpg';

(async () => {
    const start = new Date().getTime(),
        positions = [];

    const browser = await puppeteer.launch({
        timeout: 15000,
        headless: false
    });
    const page = await browser.newPage();

    await page.goto('https://www.vvic.com/gz?noCity=1');

    const input = await page.$('#up-content > div > input[type="file"]');
    await input.uploadFile(`${__dirname}/images/test.jpg`);
    // await page.evaluate(() => document.querySelector('#sm-widget-upload > div > input[type="file"]')
    //     .addEventListener('click', e => e.preventDefault()));
    // await page.click('#sm-widget-picbtn');

    await page.waitFor(5000);

    console.log(page.url());

    await browser.close();

    console.log(`档口：${positions}`);
    console.log(`总耗时：${((new Date().getTime() - start) / 1000).toFixed(2)}s`);
})();